package com.example.flink;

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.api.common.serialization.DeserializationSchema;
import org.apache.flink.streaming.connectors.kafka.KafkaDeserializationSchema;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
import org.apache.flink.streaming.api.functions.sink.SinkFunction;
import org.apache.flink.api.common.functions.MapFunction;

import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.util.Properties;

public class FlinkConfig {
    public static void configureFlinkStream() throws Exception {

        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();


        Properties properties = new Properties();
        properties.put("bootstrap.servers", "localhost:9092");
        properties.put("group.id", "flink-consumer");
        properties.put("auto.offset.reset", "earliest");

        FlinkKafkaConsumer<String> kafkaConsumer = new FlinkKafkaConsumer<>(
                "test",
                new SimpleStringSchema(),
                properties
        );


        DataStream<String> stream = env.addSource(kafkaConsumer);


        DataStream<String> classifiedStream = stream.map(new MapFunction<String, String>() {
            @Override
            public String map(String value) throws Exception {

                if (value.contains("important")) {
                    return "important: " + value;
                } else {
                    return "not important: " + value;
                }
            }
        });


        classifiedStream.print();


        env.execute("Flink Kafka Consumer");
    }
}
